<!DOCTYPE html>


<html lang="zh-CN">
  

    <head>
      <meta charset="utf-8" />
       
      <meta name="keywords" content="ES6、JS" />
       
      <meta name="description" content="ES6、JS" />
      
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>路由 |  君书</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="君书" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-路由"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  路由
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2022/06/13/%E8%B7%AF%E7%94%B1/" class="article-date">
  <time datetime="2022-06-13T01:56:59.000Z" itemprop="datePublished">2022-06-13</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.3k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">6 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="一-路由使用"><a href="#一-路由使用" class="headerlink" title="一. 路由使用"></a>一. 路由使用</h2><h3 id="1-嵌套路由"><a href="#1-嵌套路由" class="headerlink" title="1. 嵌套路由"></a>1. 嵌套路由</h3><p>使用嵌套路由时，注意 <code>v16</code>  版本需要在父路由组件中给匹配的子路由视图预留出口。<code>V16</code> 提供了 <code>Outlet</code> 组件实现路由出口功能。</p>
<h3 id="2-Index-Route"><a href="#2-Index-Route" class="headerlink" title="2. Index Route"></a>2. Index Route</h3><p>当访问父路由中的某个具体子路由时，对应的子路由视图会替换父路由视图中 <code>Outlet</code> 。但若希望在访问父路由时就可以有默认视图替换<code>Outlet</code> ，我们可以在父路由中再嵌套一个子路由，该子路由需要有以下特点：</p>
<ol>
<li>不需要 <code>path</code> 属性（实际上它的 path 值和父路由 path 是一样的）</li>
<li>需要添加 <code>index</code> 属性</li>
</ol>
<p>我们也可以从以下几点了解 <code>Index Route</code></p>
<ul>
<li>Index routes render in the parent routes outlet at the parent route’s path.</li>
<li>Index routes match when a parent route matches but none of the other children match.</li>
<li>Index routes are the default child route for a parent route.</li>
<li>Index routes render when the user hasn’t clicked one of the items in a navigation list yet.</li>
</ul>
<h3 id="3-404路由"><a href="#3-404路由" class="headerlink" title="3. 404路由"></a>3. 404路由</h3><p>当路由未匹配到对应视图时，页面会显示空白。为了友好提示用户，可以添加 <code>404</code> 路由。404 路由有以下特点：</p>
<ol>
<li>路由的 <code>path: *</code></li>
<li>建议 404 路由需要放在所有路由后面（虽然 <code>v16</code> 对 404 路由放置位置无要求，但根据代码约定风格，建议放在最后面）</li>
</ol>
<h3 id="4-Active-Link"><a href="#4-Active-Link" class="headerlink" title="4. Active Link"></a>4. Active Link</h3><p>通过点击 <code>Link</code> 组件跳转路由时，若想为当前 <code>Link</code> 添加激活状态，建议使用 <code>NavLink</code> 替换 <code>Link</code> 组件。NavLink 有以下特点：</p>
<ol>
<li>style 属性可以是一个接收 <code>isActive</code> 参数的函数，该函数返回一个对象，<code>isActive</code> 表示当前 link 是否被激活</li>
<li>其他属性配置和 <code>Link</code> 组件相同</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* <span class="doctag">@description </span>路由配置组件</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">import</span> &#123; useRoutes &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Home <span class="keyword">from</span> <span class="string">&#x27;../views/index&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Login <span class="keyword">from</span> <span class="string">&#x27;../views/login&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Registry <span class="keyword">from</span> <span class="string">&#x27;../views/registry&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Page404 <span class="keyword">from</span> <span class="string">&#x27;../views/404&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Expenses <span class="keyword">from</span> <span class="string">&quot;../views/expenses&quot;</span></span><br><span class="line"><span class="keyword">import</span> Invoices <span class="keyword">from</span> <span class="string">&quot;../views/invoices&quot;</span></span><br><span class="line"><span class="keyword">import</span> Invoice <span class="keyword">from</span> <span class="string">&quot;../views/invoices/Detail&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> HomeIndex = <span class="function">() =&gt;</span> (<span class="xml"><span class="tag">&lt;<span class="name">main</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span> <span class="attr">padding:</span> &quot;<span class="attr">1rem</span>&quot; &#125;&#125;&gt;</span><span class="tag">&lt;<span class="name">p</span>&gt;</span>Select a Tab<span class="tag">&lt;/<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">main</span>&gt;</span></span>)</span><br><span class="line"><span class="keyword">const</span> InvoiceIndex = <span class="function">() =&gt;</span> (<span class="xml"><span class="tag">&lt;<span class="name">main</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span> <span class="attr">padding:</span> &quot;<span class="attr">1rem</span>&quot; &#125;&#125;&gt;</span><span class="tag">&lt;<span class="name">p</span>&gt;</span>Select an invoice<span class="tag">&lt;/<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">main</span>&gt;</span></span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> routes = <span class="built_in">Object</span>.freeze([</span><br><span class="line">  &#123;</span><br><span class="line">    element: <span class="xml"><span class="tag">&lt;<span class="name">Home</span>/&gt;</span></span>,</span><br><span class="line">    path: <span class="string">&#x27;/&#x27;</span>,</span><br><span class="line">    auth: <span class="literal">true</span>,</span><br><span class="line">    children: [</span><br><span class="line">      &#123;</span><br><span class="line">        element: <span class="xml"><span class="tag">&lt;<span class="name">HomeIndex</span> /&gt;</span></span>,</span><br><span class="line">        index: <span class="literal">true</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        element: <span class="xml"><span class="tag">&lt;<span class="name">Expenses</span> /&gt;</span></span>,</span><br><span class="line">        path: <span class="string">&#x27;expenses&#x27;</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        element: <span class="xml"><span class="tag">&lt;<span class="name">Invoices</span> /&gt;</span></span>,</span><br><span class="line">        path: <span class="string">&#x27;invoices&#x27;</span>,</span><br><span class="line">        children: [</span><br><span class="line">          &#123;</span><br><span class="line">            element: <span class="xml"><span class="tag">&lt;<span class="name">InvoiceIndex</span> /&gt;</span></span>,</span><br><span class="line">            index: <span class="literal">true</span>,</span><br><span class="line">          &#125;,</span><br><span class="line">          &#123;</span><br><span class="line">            element: <span class="xml"><span class="tag">&lt;<span class="name">Invoice</span> /&gt;</span></span>,</span><br><span class="line">            path: <span class="string">&#x27;:invoiceId&#x27;</span>,</span><br><span class="line">          &#125;,</span><br><span class="line">        ]</span><br><span class="line">      &#125;,</span><br><span class="line">    ],</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    element: <span class="xml"><span class="tag">&lt;<span class="name">Login</span> /&gt;</span></span>,</span><br><span class="line">    path: <span class="string">&#x27;/login&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    element: <span class="xml"><span class="tag">&lt;<span class="name">Registry</span> /&gt;</span></span>,</span><br><span class="line">    path: <span class="string">&#x27;/registry&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    element: <span class="xml"><span class="tag">&lt;<span class="name">Page404</span> /&gt;</span></span>,</span><br><span class="line">    path: <span class="string">&#x27;*&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">])</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Router = <span class="function">() =&gt;</span> useRoutes(routes)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* <span class="doctag">@description </span>App组件</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">import</span> &#123; BrowserRouter &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; Router &#125; <span class="keyword">from</span> <span class="string">&#x27;./router&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;BrowserRouter&gt;</span><br><span class="line">      &lt;Router /&gt;</span><br><span class="line">    &lt;/BrowserRouter&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App</span><br></pre></td></tr></table></figure>



<h2 id="二-路由守卫"><a href="#二-路由守卫" class="headerlink" title="二. 路由守卫"></a>二. 路由守卫</h2><p>建议在父组件渲染子路由视图组件的位置进行权限判断：</p>
<ol>
<li>有权限，渲染 <code>Outlet</code> 组件</li>
<li>无权限，跳转登录页面</li>
</ol>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* <span class="doctag">@description </span>路由鉴权</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">import</span> &#123; useEffect &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; Outlet, useNavigate, useLocation &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> useLoginInfo <span class="keyword">from</span> <span class="string">&#x27;../../hooks/useLoginInfo&#x27;</span></span><br><span class="line"><span class="keyword">import</span> useCurrentRoute <span class="keyword">from</span> <span class="string">&#x27;../../hooks/useCurrentRoute&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">AuthRoute</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; auth &#125; = useCurrentRoute()  </span><br><span class="line">  <span class="keyword">const</span> userInfo = useLoginInfo()  </span><br><span class="line">  <span class="keyword">const</span> location = useLocation()  </span><br><span class="line">  <span class="keyword">const</span> navigate = useNavigate()</span><br><span class="line">  </span><br><span class="line">  useEffect(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span>(auth &amp;&amp; !userInfo) &#123;</span><br><span class="line">      navigate(<span class="string">`/login?go=<span class="subst">$&#123;location.pathname&#125;</span>`</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [auth, location.pathname])</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">Outlet</span> /&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* <span class="doctag">@description </span>路由视图出口引用鉴权组件</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">import</span> &#123; Link &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> AuthRoute <span class="keyword">from</span> <span class="string">&#x27;../router/authRoute&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Home</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;&gt;</span><br><span class="line">      &lt;nav</span><br><span class="line">        style=&#123;&#123;</span><br><span class="line">          borderBottom: <span class="string">&quot;solid 1px&quot;</span>,</span><br><span class="line">          paddingBottom: <span class="string">&quot;1rem&quot;</span>,</span><br><span class="line">        &#125;&#125;</span><br><span class="line">      &gt;</span><br><span class="line">        &lt;Link to=<span class="string">&quot;/invoices&quot;</span>&gt;Invoices&lt;/Link&gt; |&#123;<span class="string">&quot; &quot;</span>&#125;</span><br><span class="line">        &lt;Link to=<span class="string">&quot;/expenses&quot;</span>&gt;Expenses&lt;/Link&gt;</span><br><span class="line">      &lt;/nav&gt;</span><br><span class="line">      &lt;div style=&#123;&#123;<span class="attr">width</span>: <span class="string">&quot;80%&quot;</span>, <span class="attr">margin</span>: <span class="string">&quot;0 auto&quot;</span>&#125;&#125;&gt;</span><br><span class="line">        &lt;AuthRoute /&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    &lt;/&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Home;</span><br></pre></td></tr></table></figure>



<h2 id="三-路由钩子"><a href="#三-路由钩子" class="headerlink" title="三. 路由钩子"></a>三. 路由钩子</h2><h3 id="1-useParams"><a href="#1-useParams" class="headerlink" title="1.  useParams"></a>1.  <code>useParams</code></h3><p>作用： 获取 URL Params</p>
<p>返回值：<code>&#123;&#125;</code></p>
<h3 id="2-useNavigate"><a href="#2-useNavigate" class="headerlink" title="2. useNavigate"></a>2. <code>useNavigate</code></h3><p>作用：获取 navigate 函数，可通过该函数触发路由跳转</p>
<p>返回值：navigate 函数</p>
<h3 id="3-useSearchParams"><a href="#3-useSearchParams" class="headerlink" title="3. useSearchParams"></a>3. <code>useSearchParams</code></h3><p>作用：获取 路由后的查询参数相关的值</p>
<p>返回值：<code>[searchParams, setSearchParams]</code></p>
<p><code>searchParams</code>  是 Map 类型的数据，存储路由后的查询参数，每个键对应的值是字符串类型；</p>
<p><code>setSearchParams(&#123;filter: &#39;xxx&#39;&#125;) </code> 是设置路由后查询参数的方法，可通过该方法为路由设置查询参数</p>
<h3 id="4-useLocation"><a href="#4-useLocation" class="headerlink" title="4. useLocation"></a>4. <code>useLocation</code></h3><p>作用：获取当前路由信息</p>
<p>返回值：如果访问的路由是 <code>http://localhost:3000/invoices/1995?filter=s</code>, 则 <code>useLocation()</code> 获取到的数据格式如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;pathname&quot;</span>: <span class="string">&quot;/invoices/1995&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;search&quot;</span>: <span class="string">&quot;?filter=s&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hash&quot;</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;state&quot;</span>: <span class="literal">null</span>,</span><br><span class="line">    <span class="attr">&quot;key&quot;</span>: <span class="string">&quot;xt191fp4&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="5-useRoutes-routes-RouteObject-locationArg-Partial-lt-Location-gt-string"><a href="#5-useRoutes-routes-RouteObject-locationArg-Partial-lt-Location-gt-string" class="headerlink" title="5. useRoutes(routes: RouteObject[], locationArg?: Partial&lt;Location&gt; | string)"></a>5. <code>useRoutes(routes: RouteObject[], locationArg?: Partial&lt;Location&gt; | string)</code></h3><p>作用：根据路由配置信息 <code>RouteObject[]</code> 生成 <code>React.ReactElement</code> 组件，具体实现函数如下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> createElement(RouteContext.Provider, &#123;</span><br><span class="line">    children: match.route.element !== <span class="literal">undefined</span> ? match.route.element : outlet,</span><br><span class="line">    value: &#123;</span><br><span class="line">        outlet,</span><br><span class="line">        matches: parentMatches.concat(matches.slice(<span class="number">0</span>, index + <span class="number">1</span>))</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> RouteContext = <span class="comment">/*#__PURE__*/</span>createContext(&#123;</span><br><span class="line">  outlet: <span class="literal">null</span>,</span><br><span class="line">  matches: []</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (process.env.NODE_ENV !== <span class="string">&quot;production&quot;</span>) &#123;</span><br><span class="line">  RouteContext.displayName = <span class="string">&quot;Route&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-useMatch-path"><a href="#6-useMatch-path" class="headerlink" title="6. useMatch(path)"></a>6. <code>useMatch(path)</code></h3><p>判断 <code>location.pathname</code> 是否与 path 匹配，若匹配，则返回路由信息</p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://xiaofujun.gitee.io/2022/06/13/%E8%B7%AF%E7%94%B1/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2022/06/22/%E4%BA%8B%E4%BB%B6%E7%8E%AF/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            事件环
          
        </div>
      </a>
    
    
      <a href="/2022/04/07/%E7%94%9F%E6%88%90%E8%84%9A%E6%89%8B%E6%9E%B6/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">生成脚手架</div>
      </a>
    
  </nav>

  
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> 小付君
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="君书"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->

<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>